package com.anolesoft.yr.mbus.code.service.impl;

import com.anolesoft.yr.mbus.code.dto.CodeMainDTO;
import com.anolesoft.yr.mbus.code.entity.CodeMainDO;
import com.anolesoft.yr.mbus.code.mapper.CodeMainMapper;
import com.anolesoft.yr.mbus.code.service.CodeMainService;
import com.anolesoft.yr.util.CommonUtils;
import com.anolesoft.yr.util.CustomBeanUtils;
import com.anolesoft.yr.util.PageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2020/9/22 11:24
 */
@Service
public class CodeMainServiceImpl implements CodeMainService {
    @Autowired
    private CodeMainMapper codeMainMapper;

    @Transactional
    @Override
    public int create(CodeMainDTO codeMainDTO) {
        return codeMainMapper.insert(CodeMainDO.builder()
                .codeName(codeMainDTO.getCodeName())
                .factoryCode(codeMainDTO.getFactoryCode())
                .objuid(codeMainDTO.getObjuid())
                .remark(codeMainDTO.getRemark())
                .modifyUserUid(codeMainDTO.getModifyUserUid())
                .modifyTime(codeMainDTO.getModifyTime())
                .geneClass(codeMainDTO.getGeneClass())
                .deleteFlag(codeMainDTO.getDeleteFlag())
                .createUserUid(codeMainDTO.getCreateUserUid())
                .createTime(codeMainDTO.getCreateTime())
                .codeDesc(codeMainDTO.getCodeDesc())
                .build());
    }

    @Transactional
    @Override
    public int modify(CodeMainDTO codeMainDTO) {
        return codeMainMapper.update(CodeMainDO.builder()
                .codeName(codeMainDTO.getCodeName())
                .factoryCode(codeMainDTO.getFactoryCode())
                .objuid(codeMainDTO.getObjuid())
                .remark(codeMainDTO.getRemark())
                .modifyUserUid(codeMainDTO.getModifyUserUid())
                .modifyTime(codeMainDTO.getModifyTime())
                .geneClass(codeMainDTO.getGeneClass())
                .deleteFlag(codeMainDTO.getDeleteFlag())
                .createUserUid(codeMainDTO.getCreateUserUid())
                .createTime(codeMainDTO.getCreateTime())
                .codeDesc(codeMainDTO.getCodeDesc())
                .build());
    }

    @Transactional
    @Override
    public int remove(String id) {
        return codeMainMapper.delete(id);
    }

    @Transactional
    @Override
    public int removeBatch(List<String> ids) {
        int res = 0;
        for (String id : ids) {
            res += codeMainMapper.delete(id);
        }
        return res;
    }

    @Override
    public List<CodeMainDTO> selectAll() {
        return codeMainMapper.selectAll().stream().map(CustomBeanUtils::codeMainDO2DTO).collect(Collectors.toList());
    }

    @Override
    public Optional<CodeMainDTO> selectOneById(String id) {
        CodeMainDO codeMainDO = codeMainMapper.selectOneById(id);
        if (codeMainDO != null) {
            return Optional.of(CustomBeanUtils.codeMainDO2DTO(codeMainDO));
        }
        return Optional.empty();
    }

    @Override
    public Optional<CodeMainDTO> selectOneByCodeName(String codeName) {
        CodeMainDO codeMainDO = codeMainMapper.selectOneByCodeName(codeName);
        if (codeMainDO != null) {
            return Optional.of(CustomBeanUtils.codeMainDO2DTO(codeMainDO));
        }
        return Optional.empty();
    }

    @Override
    public PageData<List<CodeMainDTO>> selectList(CodeMainDTO codeMainDTO, Integer limit, Integer curr) {
        CodeMainDO codeItemDO = buildCondition(codeMainDTO);
        Page<CodeMainDO> page = new Page<>(curr, limit);
        List<CodeMainDTO> codeItemDTOS = codeMainMapper.selectPageList(codeItemDO, page).stream().map(CustomBeanUtils::codeMainDO2DTO).collect(Collectors.toList());
        return new PageData<>((int) page.getTotal(), codeItemDTOS);
    }

    private CodeMainDO buildCondition(CodeMainDTO codeMainDTO) {
        return CodeMainDO.builder()
                .factoryCode(CommonUtils.stringNull(codeMainDTO.getFactoryCode()))
                .codeName(CommonUtils.warpStringNull(codeMainDTO.getCodeName()))
                .objuid(CommonUtils.stringNull(codeMainDTO.getObjuid()))
                .build();
    }
}
